package org.apache.zookeeper.server.paxos2;

import java.util.LinkedList;

/**
 * Created by IntelliJ IDEA.
 * User: iodine
 * Date: Sep 26, 2011
 * Time: 10:28:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class MemoryBuffer {
    private static LinkedList<byte[]> readBuffers = new LinkedList<byte[]>();

    public synchronized static byte[] allocateBuffer(){
        if(readBuffers.size()>0){
            return readBuffers.removeFirst();
	}
        else
            return new byte[Configuration.blockSize];
    }

    public synchronized static void deallocateBuffer(byte[] buf){
	if(buf==null||buf.length!=Configuration.blockSize)
	    return;
        if(readBuffers.size()<512*1024*1024/Configuration.blockSize){
            readBuffers.addLast(buf);
	}
    }
}
